package com.gurun.codes.business.natural_gas;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.gurun.base.DebounceObservableTransformer;
import com.gurun.base.utils.ToastManager;
import com.gurun.codes.R;
import com.gurun.codes.base.BaseRecyclerViewActivity;
import com.gurun.codes.databinding.ActivityCommonBinding;
import com.gurun.codes.databinding.CommonContentLayoutBinding;
import com.gurun.model.bean.common.GetDeviceAlarmInfoListResultBean;
import com.gurun.model.bean.natural_gas.GetNaturalGasInfoResultBean;
import com.jakewharton.rxbinding2.view.RxView;

import java.util.List;

import io.reactivex.functions.Consumer;

/**
 * 天然气报警器
 */
public class NaturalGasActivity extends BaseRecyclerViewActivity implements NaturalGasActivityContract.NaturalGasActivityView {

    private static final String ARG_DEVICE_ID = "device_id";
    private ActivityCommonBinding mBinding;
    private CommonContentLayoutBinding mContentBinding;
    private long mDeviceId = 0;
    private ProgressDialog mDialog;
    private NaturalGasActivityContract.NaturalGasActivityViewPresenter mPresenter;

    public static void startActivity(Context context, long deviceId) {
        Intent intent = new Intent(context, NaturalGasActivity.class);
        intent.putExtra(ARG_DEVICE_ID, deviceId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_common);
        mPresenter = new NaturalGasActivityContract.NaturalGasActivityViewPresenter(this);
        mPresenter.bind(this);
        initView();
        initDatas();
        initEvent();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unbind();
        }
    }

    @Override
    protected void initView() {
        mBinding.rlRoot.setBackgroundResource(R.mipmap.bg_natural_gas);
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        mContentBinding = DataBindingUtil.inflate(layoutInflater, R.layout.common_content_layout, mBinding.llContent, true);
        initRevyclerView(mContentBinding.recyclerview, mContentBinding.llEmptyview);
        mContentBinding.layoutEmpty.ivImage.setImageResource(R.mipmap.ic_empty_natural_gas);
        mContentBinding.tvNextPage.requestFocus();
        mContentBinding.tvTitle.setText(R.string.home_natural_gas);
    }

    @Override
    protected void initDatas() {
        mDeviceId = getIntent().getLongExtra(ARG_DEVICE_ID, 0);
        mPresenter.getDeviceAlarmInfo(mDeviceId);
    }

    private void setUpViewByDatas(boolean isNextPage, boolean isPrePage) {
        List<Object> datas = mPresenter.getAlarmDatas(isNextPage, isPrePage, mDeviceId);
        if (datas == null) {
            ToastManager.show(mContext, R.string.common_no_next_page);
        } else {
            if (datas.size() > 0) {
                mDatas.clear();
                mDatas.addAll(datas);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void initEvent() {
        RxView.clicks(mContentBinding.tvNextPage)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        setUpViewByDatas(true, false);
                    }
                });

        RxView.clicks(mContentBinding.tvPrePage)
                .compose(new DebounceObservableTransformer<Object>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (!mPresenter.isFirstPage()) {
                            setUpViewByDatas(false, true);
                        } else {
                            ToastManager.show(mContext, R.string.common_no_pre_page);
                        }
                    }
                });
    }

    @Override
    public void onLoading() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(mContext);
            mDialog.setMessage(getString(R.string.common_loading));
        }
        mDialog.show();
    }

    @Override
    public void onLoaded(GetDeviceAlarmInfoListResultBean datas) {
        if (datas != null) {
            mPresenter.parseServerDatas(datas.getAlarmList());
            setUpViewByDatas(false, false);
        }
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(mContext, msg);
    }

    @Override
    public void onComplete() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void resetListDatas() {

    }
}
